package client;

import java.io.InputStream;

/**
 *
 * @author BeTo
 */
public class videoclientp2p {

    String VLCroute;
    String port;

    public videoclientp2p(String VLCroute, String port) {
        this.VLCroute = VLCroute;
        this.port = port;
    }

    public void viewvideo() {
        try {
            String command = VLCroute + " rtp://@:" + port;

            /*At this point we create a new process to execute the command
             * needed to execute a new P2P VLC client.
             */
            final Process view = Runtime.getRuntime().exec(command);

            /*We create two new Threads to control the execution of the Process
             * showing out the errors and inputs it has. Finally, we make the
             * program wait until the Process finish.
             */
            new Thread() {

                public void run() {
                    try {
                        InputStream is = view.getInputStream();
                        byte[] buffer = new byte[1024];
                        for (int count = 0; (count = is.read(buffer)) >= 0;) {
                            System.out.write(buffer, 0, count);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();

            new Thread() {

                public void run() {
                    try {
                        InputStream is = view.getErrorStream();
                        byte[] buffer = new byte[1024];
                        for (int count = 0; (count = is.read(buffer)) >= 0;) {
                            System.err.write(buffer, 0, count);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();

            int returnCode = view.waitFor();
            System.out.println("Return code = " + returnCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
